//
//  AlertOperationView.m
//  ShouKuang
//
//  Created by toro宇 on 2019/5/31.
//  Copyright © 2019 CodeYY. All rights reserved.
//

#import "AlertOperationView.h"
@interface AlertOperationView()
@property(nonatomic, strong)SureBtnBlock sureBtnBlock;
@property(nonatomic, strong)CancleBtnBlock cancleBtnBlock;
@property(nonatomic, copy)NSString *imgName;
@property(nonatomic, copy)NSString *titleStr;


@end

@implementation AlertOperationView

+(void)alertOperationView:(NSString *)imgName titleStr:(NSString *)titleStr sureBtnBlock:(SureBtnBlock)sureBtnBlock cancleBtnBlock:(CancleBtnBlock)cancleBtnBlock
{
    AlertOperationView *alertView = [AlertOperationView new];
    alertView.imgName = imgName;
    alertView.titleStr = titleStr;
    alertView.sureBtnBlock  = sureBtnBlock;
    alertView.cancleBtnBlock = cancleBtnBlock;
    [alertView initContentView];
    alertView.frame = CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height);
    [[UIApplication sharedApplication].windows.firstObject addSubview:alertView];
    
    if (!sureBtnBlock) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (alertView) {
                [alertView removeFromSuperview];
            }
        });
    }
}
-(void)initContentView
{
    __weak typeof(self) weakSelf = self;
    UIButton *shadowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:shadowBtn];
    shadowBtn.alpha = 0.3;
    [shadowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    shadowBtn.backgroundColor = [UIColor blackColor];
    [[shadowBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        [weakSelf removeFromSuperview];
    }];
    
    UIView *contentView = [UIView new];
    [self addSubview:contentView];
    contentView.layer.masksToBounds = YES;
    contentView.layer.cornerRadius = 8;
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.centerY.offset(-50);
        make.right.offset(-50);
        make.left.offset(50);
    }];
    contentView.backgroundColor = [UIColor whiteColor];
    UIImageView *imgView;
    if (_imgName) {
        imgView = [UIImageView new];
        [contentView addSubview:imgView];
        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(30);
            make.centerX.offset(0);
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];
        imgView.image = [UIImage imageNamed:_imgName];
    }
    UILabel *lab ;
    if (_titleStr) {
        lab = [UILabel new];
        [contentView addSubview:lab];
        lab.textColor = [UIColor darkGrayColor];
        lab.numberOfLines = 0;
        lab.textAlignment = NSTextAlignmentCenter;
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            if (weakSelf.imgName) {
                make.top.equalTo(imgView.mas_bottom).offset(10);
            }else{
                make.top.offset(30);
            }
            make.left.offset(50);
            make.right.offset(-50);
        }];
        lab.text = _titleStr;
    }
    
    if (!_sureBtnBlock && !_cancleBtnBlock) {
        [lab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.offset(-30);
        }];
    }else if (_sureBtnBlock && !_cancleBtnBlock){
        UIView *line = [UIView new];
        [contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.offset(0);
            make.top.equalTo(lab.mas_bottom).offset(30);
            make.height.mas_equalTo(1);
        }];
        line.backgroundColor = [UIColor lightGrayColor];
        
        UIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom];
        [contentView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.offset(0);
            make.height.mas_equalTo(50);
            make.top.equalTo(line.mas_bottom);
        }];
        [btn setTitle:@"确定" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
            weakSelf.sureBtnBlock();
            [weakSelf removeFromSuperview];

        }];
        
    }else if (_sureBtnBlock && _cancleBtnBlock){
        UIView *line = [UIView new];
        [contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.offset(0);
            make.top.equalTo(lab.mas_bottom).offset(30);
            make.height.mas_equalTo(1);
        }];
        line.backgroundColor = [UIColor lightGrayColor];
        
        UIButton *leftBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        [contentView addSubview:leftBtn];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.offset(0);
            make.height.mas_equalTo(50);
            make.top.equalTo(line.mas_bottom);
        }];
        [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
        [leftBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [[leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
            weakSelf.cancleBtnBlock();
            [weakSelf removeFromSuperview];

        }];
        
        UIButton *rightBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        [contentView addSubview:rightBtn];
        [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.offset(0);
            make.left.equalTo(leftBtn.mas_right);
            make.size.equalTo(leftBtn);
            make.top.equalTo(line.mas_bottom);
        }];
        [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
        [rightBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [[rightBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
            weakSelf.sureBtnBlock();
            [weakSelf removeFromSuperview];

        }];
        
        UIView *sepeline = [UIView new];
        [rightBtn addSubview:sepeline];
        [sepeline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(rightBtn);
            make.width.mas_equalTo(1);
        }];
        sepeline.backgroundColor = [UIColor lightGrayColor];
    }
  
}

@end
